package com.teach.javafxclient.controller.base;

import com.teach.javafxclient.Globalvar;
import com.teach.javafxclient.MainApplication;
import com.teach.javafxclient.request.HttpRequestUtil;
import com.teach.javafxclient.util.HttpUtil;
import javafx.beans.value.ChangeListener;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.FlowPane;
import javafx.stage.Stage;
import kong.unirest.json.JSONArray;
import kong.unirest.json.JSONObject;

import java.io.IOException;

/**
 * @author Liang Jianlin
 * @date 2024-05-31 16:19
 */
public class CourseListController {
    public FlowPane courseContainer;
    public void set(String teacherId) throws IOException {
        JSONArray JA = HttpUtil.get(Globalvar.baseUrl+"/api/teachers/"+teacherId+"/courses")
                .header("Accept", "application/json")
                .asJson()
                .getBody()
                .getArray();
        for (int i = 0; i < JA.length(); i++) {
            JSONObject JO = JA.getJSONObject(i);
            FXMLLoader fxmlLoader = new FXMLLoader(MainApplication.class.getResource("base/academic-performance-item.fxml"));
            Parent root = fxmlLoader.load();
            AcademicPerformanceItemController APIC = fxmlLoader.getController();
            APIC.setItem(JO.getString("name"), JO.getString("teacherName"),String.valueOf(JO.get("credit")),JO.getString("termName"),JO.getBoolean("ended"),"");

            root.setOnMouseClicked(e->{
                FXMLLoader fxmlLoader2 = new FXMLLoader(MainApplication.class.getResource("base/course-info.fxml"));
                Parent root2;
                try {
                    root2 = fxmlLoader2.load();
                } catch (IOException ex) {
                    throw new RuntimeException(ex);
                }
                CourseInfoController CIC = fxmlLoader2.getController();
                CIC.setCourseInfo(String.valueOf(JO.get("courseId")));
                Stage stage = new Stage();
                Scene scene = new Scene(root2, 600.0, 800.0);
                stage.setTitle("课程信息");
                stage.setScene(scene);
                stage.show();
                stage.setOnCloseRequest(event -> {
                    HttpRequestUtil.close();
                });
            });


            courseContainer.getChildren().add(root);
        }
//        courseContainer.setMaxWidth(700);
//        courseContainer.setMinWidth(700);
    }
}
